<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 行为控制器
 * @author huajie <banhuajie@163.com>
 */
class ActionController extends CommonController
{


    /**
     * 用户行为列表
     */
    public function index()
    {
        //获取列表数据
        $Action = M('Action')->where(array('status' => array('gt', -1)));
        $list = $this->lists($Action);
        $this->assign('list', $list);
        $this->display();
    }

    /**
     * 新增行为
     */
    public function add()
    {
        if (IS_POST) {
            $res = D('Action')->update();
            if (!$res) {
                $this->error(D('Action')->getError());
            } else {
                $this->success($res['id'] . '新增成功！');
            }
        }
        $this->display();
    }

    /**
     * 编辑行为
     */
    public function edit()
    {
        if (IS_POST) {
            $res = D('Action')->update();
            if (!$res) {
                $this->error(D('Action')->getError());
            } else {
                $this->success($res['id'] . '新增成功！');
            }
        }else{
            $id = I('id');
            $info = M('Action')->find($id);
            $this->assign('data', $info);
            $this->display();
        }
    }

    /**
     * 行为日志列表
     * @author huajie <banhuajie@163.com>
     */
    public function actionLog()
    {
        //获取列表数据
        $map = $this->_search();
        $map['al.status'] = array('gt', -1);
        $prefix = C('DB_PREFIX');
        $model = M()->table($prefix . 'action_log al')
            ->join($prefix . 'member m ON al.user_id=m.uid', 'LEFT');
        $list = $this->lists($model, $map, 'al.id desc', 'al.*');
//        int_to_string($list);
//        foreach ($list as $key=>$value){
//            $model_id                  =   get_document_field($value['model'],"name","id");
//            $list[$key]['model_id']    =   $model_id ? $model_id : 0;
//        }
        foreach($list as &$arr){
            if(in_array($arr['model'], array('article', 'category'))){
                $arr['modelName'] = $arr['model']=='article' ? 'document' : $arr['model'];
                $arr['title'] = M($arr['modelName'])->where(array('id'=>$arr['record_id']))->getField('title');
            }
        }
        $this->assign('_list', $list);
        $this->display();
    }

    /**
     * 查看行为日志
     * @author huajie <banhuajie@163.com>
     */
    public function editActionLog($id = 0)
    {
        empty($id) && $this->error('参数错误！');

        $info = M('ActionLog')->field(true)->find($id);

        $this->assign('info', $info);
        $this->meta_title = '查看行为日志';
        $this->display();
    }

    /**
     * 删除日志
     * @param mixed $ids
     * @author huajie <banhuajie@163.com>
     */
    public function removeActionLog($ids = 0)
    {
        empty($ids) && $this->error('参数错误！');
        if (is_array($ids)) {
            $map['id'] = array('in', $ids);
        } elseif (is_numeric($ids)) {
            $map['id'] = $ids;
        }
        $res = M('ActionLog')->where($map)->delete();
        if ($res !== false) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    /**
     * 清空日志
     */
    public function clearActionLog()
    {
        $res = M('ActionLog')->where('1=1')->delete();
        if ($res !== false) {
            $this->success('日志清空成功！');
        } else {
            $this->error('日志清空失败！');
        }
    }

}
